package com.bana.fvs.app.main.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseFragment;
import com.bana.fvs.app.main.adapter.Tab2CommonAdapter;
import com.bana.fvs.app.main.view.CommonHeadRefreshView;
import com.bana.fvs.app.module.tab2.Tab2CommonFragment;
import com.bana.fvs.app.mvp.contract.EquipmentContract;
import com.bana.fvs.app.mvp.presenter.EquipmentPresenter;
import com.wei.commonlibrary.manager.RxBus;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.GlobalContext;
import com.wei.commonlibrary.utils.ToastUtil;

import butterknife.BindView;
import rx.Observable;
import rx.functions.Action1;

/**
 * Author : yanftch
 * Date : 2018/2/7
 * Time : 10:26
 * Desc : 设备监测Fragment---第二个TAB
 */

public class EquipmentFragment extends BaseFragment<EquipmentPresenter> implements EquipmentContract.AbsEquipmentView {
    private static final String TAG = "dah_EquipmentFragment";
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private Tab2CommonAdapter adapter;
    //顶部通用刷新View
    private CommonHeadRefreshView mCommonHeadRefreshView;


    @Override
    protected int setLayout() {
        return R.layout.layout_fragment_equipment;
    }

    @Override
    protected void initView(View rootView) {
        adapter = new Tab2CommonAdapter(mContext, getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(3);
        mTabLayout.setupWithViewPager(mViewPager);
        mCommonHeadRefreshView = new CommonHeadRefreshView(mContext, rootView.findViewById(R.id.commonHeadViewId), "");
    }

    private static final String DEBUG_TAG = "debug_EquipmentFragment";

    @Override
    protected void initWidget() {
        mCommonHeadRefreshView.setITimeRefresh(() -> {

            Fragment fragment = (Fragment) adapter.instantiateItem(mViewPager, mViewPager.getCurrentItem());
            if (fragment instanceof Tab2CommonFragment) {
                ((Tab2CommonFragment) fragment).http_getData();
            }
        });
        Observable<String> updata_time = RxBus.getInstance().register("update_time");
        RxBus.getInstance().onEvent(updata_time, new Action1<Object>() {
            @Override
            public void call(Object o) {
                if (o == null) return;
                mCommonHeadRefreshView.refreshData((String) o);
            }
        });
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    protected void initPrsenter() {
        mFPresenter.setView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getInstance().unRegister("update_time");
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(GlobalContext.context, e.getMessage());
    }

    @Override
    public void onResponse(String s) {

    }
}
